<?

if ($DATA['component']['id']) {


	$_id_gallery	= D_GALLERY_DATA;
	$_gallery		= (int) param('gallery');


	$s = array_extend( array(
		'path' => 'catalog', 'w' => 128, 'h' => 96, 'param' => 'id', 'caption' => '', 'lw' => 400, 'lh' => 300,
		'gal_path' => 'catalog/gal', 'gal_tn' => FP_TN_DIR, 'gal_w' => 800, 'gal_h' => 600, 'gal_tnw' => 96, 'gal_tnh' => 96
	), $DATA['component']['settings']);
	
	$s['gal_path'] = createdir(FP_UPLOAD_PATH."$s[gal_path]-$_gallery/");
	$s['gal_w'] = (int)$s['gal_w'];
	$s['gal_h'] = (int)$s['gal_h'];
	$s['gal_tnw'] = (int)$s['gal_tnw'];
	$s['gal_tnh'] = (int)$s['gal_tnh'];

	createdir("$s[gal_path]$s[gal_tn]");
	

	// fix crumbs
	$temp = end(array_keys($DATA['crumb']));
	$DATA['crumb'][$temp]['name'] = 'Галерея';



	if ($d = h(param('delete'))) {	// *** delete photo
	
		if ((strpos($d, '..') === false) && is_file($_SERVER['DOCUMENT_ROOT'].$s['gal_path'].$d)) {
			@unlink($_SERVER['DOCUMENT_ROOT']."$s[gal_path]$d");
			@unlink($_SERVER['DOCUMENT_ROOT']."$s[gal_path]$s[gal_tn]$d");

			dataset('db_delete', tb('data'), "id_data=$_id_gallery AND key=\"{$s['gal_path']}\" AND name='$d'");
		}
	
		header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'delete')));
		exit;
	}


	$DATA['gallery'] = datafetch_array('gallery2', $s['gal_path']);	// load existing images list
//	uksort($DATA['gallery'], 'fn_numeric_compare');
	
	switch (action(array('photo_add'))) {
	
		case 'photo_add':
	
				if ($temp = takeImage('image', $s['gal_w'], $s['gal_h'], $s['gal_path'], $s['gal_tnw'], $s['gal_tnh'], $s['gal_path'].$s['gal_tn'])) {
					$DATA['gallery'][$temp['filename']] = array('w' => $temp['w'], 'h' => $temp['h'], 'fn' => $temp['filename'], 'tn' => $s['gal_tn'].$temp['filename']);

					$max = ($m = datafetch('db_item', tb('data'), "id_data=$_id_gallery AND key=\"{$s['gal_path']}\"", "MAX(id_index) as m")) ? $m['m']+1 : 1;
					dataset('db_insert', tb('data'), array('id_data' => $_id_gallery, 'key' => $s['gal_path'], 'name' => $temp['filename'], 'id_index' => $max) );
				}
						
				return false;
	}


	return false;

} else exit;

?>